package com.gjt.clientSocket;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.*;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * Created with Intel IDEA.
 *  流 转换成 xml
 * @Auther: gjt
 * @Date: 2024/10/27 9:11
 */
public class InputStreamToXml {
    public static void main(String[] args) {


        try(ServerSocketChannel open = ServerSocketChannel.open();) {

            // 从通道（应该是任何通道）中获取流
            FileChannel fileChannel = FileChannel.open(Paths.get("D:\\Idea\\项目\\myboot\\socket\\src\\main\\resources\\test.xml"));
            InputStream inputStream1 = Channels.newInputStream(fileChannel);

            //从流中获取通道(只有读取的方法，基本没有别的方法)
            InputStream inputStream2 = Files.newInputStream(Paths.get("D:\\Idea\\项目\\myboot\\socket\\src\\main\\resources\\test.xml"));
            ReadableByteChannel readableByteChannel = Channels.newChannel(inputStream2);


            SocketChannel accept = open.accept();
            XMLReader xmlReader = XMLReaderFactory.createXMLReader();

            // 通过通道获取输入流
            InputStream inputStream = Channels.newInputStream(accept);

            InputSource inputSource = new InputSource(inputStream);
            // 支支持InputSource 和 String 类型
            xmlReader.parse(inputSource);
        } catch (SAXException | IOException e) {
            throw new RuntimeException(e);
        }
    }
}
